﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.Business.Prod;
using ESOL.Business.Mst;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmManageWorkingGroup : BaseForm.frmBaseMdiBusinessForm
    {
        #region vars
        private MstWorkingGroupEntity SelectedMstWorkingGroup;
        #endregion

        #region ctors
        public frmManageWorkingGroup()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void BindComobox()
        {
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colGender, "GENDER", false);
        }

        private string GetCondition()
        {
            string Condition = "";
            if (!string.IsNullOrEmpty(txtGroupName.Text))
            {
                Condition += " AND MWG.GROUP_NAME LIKE '%" + txtGroupName.Text + "%' ";
            }
            return Condition;
        }

        private void LoadGridMain()
        {
            string Condition = GetCondition();
            DataTable dt = MstWorkingGroupBs.GetDtMstWorkingGroupSummaryByCondition(Condition, txtRowCount.Text, "");
            gvMain.DataSource = dt;
        }
        private void LoadGridDetail()
        {
            if (SelectedMstWorkingGroup == null)
                return;
            string Condition = " AND MWG.GROUP_ID='" + SelectedMstWorkingGroup.GroupId + "' ";
            DataTable dt = MstWorkingGroupBs.GetDtMstWorkingGroupByCondition(Condition, "");
            gvDetail.DataSource = dt;
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComobox();
            LoadGridMain();
            LoadGridDetail();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow drv = gridView1.GetDataRow(e.RowHandle);
            SelectedMstWorkingGroup = new MstWorkingGroupEntity(drv);
            if (SelectedMstWorkingGroup == null)
            {
                return;
            }
            LoadGridDetail();
        }

        protected override void Click_Query()
        {
            base.Click_Query();
            LoadGridMain();
        }

        protected override void Click_View()
        {
            if (SelectedMstWorkingGroup == null)
            {
                MessageBox.Show("请选择一个工作组！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainWorkingGroup frm = new frmMaintainWorkingGroup();
            frm.ThisActionType = ActionType.View;
            frm.SelectedMstWorkingGroup = SelectedMstWorkingGroup;
            frm.ShowDialog();
        }

        protected override void Click_New()
        {
            frmMaintainWorkingGroup frm = new frmMaintainWorkingGroup();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                LoadGridMain();
                LoadGridDetail();
            }
        }

        protected override void Click_Modify()
        {
            if (SelectedMstWorkingGroup == null)
            {
                MessageBox.Show("请选择一个工作组！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainWorkingGroup frm = new frmMaintainWorkingGroup();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedMstWorkingGroup = SelectedMstWorkingGroup;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                LoadGridMain();
                LoadGridDetail();
            }
        }

        protected override void Click_Delete()
        {
            if (SelectedMstWorkingGroup == null)
            {
                MessageBox.Show("请选择一个工作组！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (MessageBox.Show("是否确认删除所选工作组！", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                if (MstWorkingGroupBs.DelWorkingGroups(SelectedMstWorkingGroup))
                {
                    LoadGridMain();
                    LoadGridDetail();
                }
                else
                {
                    MessageBox.Show("删除失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
        }

        #endregion
    }
}
